var ById=/#{1}/i;
var ByClass=/\.{1}/i;
function getElementByLevel(x){
  var result;
  var checka=/(\w)(\s){1}(\w|\.|#)/i;
  var checkb=/(\w|\s)(>){1}(\w|\s|\.|#)/i;
  var checkc=/(\w|\s)(\+){1}(\w|\s|\.|#)/i;
  var checkd=/(\w|\s)(~){1}(\w|\s|\.|#)/i;
      //判断属于哪种层次选择器
      if(checka.test(x)){
          result=houdai(x);
      }
      else if(checkb.test(x)){
            result=zidai(x);
      }
      else if(checkc.test(x)){
            result=jinlin(x);
      }
      else if(checkd.test(x)){
            result=zhihousuoyou(x);
      }
      else{
            result="找不到对象";
      }
      return result;
}

function judgeICT(x){
     var ById=/#{1}/i;
     var ByClass=/\.{1}/i;
     var result,replace;
     if(ById.test(x)){
         var replace=x.replace(/#/,"").replace(/\s/,"");
         result=FunById(replace);
     }else if(ByClass.test(x)){
         var replace=x.replace(/\./,"").replace(/\s/,"");
         result=FunByClass(replace);
     }else{
         var replace=x.replace(/\s/,"");
         result=FunByTag(replace);
     }
     return result;
}
//下面三个函数分别按照id/class/tag查找元素
//参数为一个时在全局范围内查找
//参数为两个时（如a/b，其中b为上一步获取的对象，a为选择器的第二部分),
function FunById(){
     var type=arguments;
     if(type.length==1){
          var a=document.getElementById(type[0]);
          return a;
     }else{
          var f=type[0].replace(/#/,"");
          var a=type[1];
          var n,m;
          var result=new Array();
          var resulta=new Array();
          if(a.length){
              for(n=0;n<a.length;n++){
                  resulta=a[n].getElementsByTagName("*");
                  for(m=0;m<resulta.length;m++){
                      if(resulta[m].getAttribute("id")==f){
                           result[result.length]=resulta[m];
                      }
                  }
              }
          }else{
              resulta=a.getElementsByTagName("*");
              for(m=0;m<resulta.length;m++){
                  if(resulta[m].getAttribute("id")==f){
                       result[result.length]=resulta[m];
                  }
              }
          }
          return result;
     }
}

function FunByClass(){
     var type=arguments;
     if(type.length==1){
          var a=document.getElementsByTagName("*");
          var ClassName=new Array();
          var n,m;
          for(n=0,m=0;n<a.length;n++){
             if(a[n].getAttribute("class")==type[0]){
               ClassName[m]=a[n];
               m++;
             }
          }
          return  ClassName;
     }else{
          var f=type[0].replace(/\./,"");
          var a=type[1];
          var n,m;
          var result=new Array();
          var resulta=new Array();
          if(a.length){
              for(n=0;n<a.length;n++){
                    resulta=a[n].getElementsByTagName("*");
                    for(m=0;m<resulta.length;m++){
                        if(resulta[m].getAttribute("class")==f){
                             result[result.length]=resulta[m];
                        }
                    }
              }
          }else{
              resulta=a.getElementsByTagName("*");
              for(m=0;m<resulta.length;m++){
                  if(resulta[m].getAttribute("class")==f){
                       result[result.length]=resulta[m];
                  }
              }
          }
          return result;
     }
}

function FunByTag(){
     var type=arguments;
     if(type.length==1){
         var a=document.getElementsByTagName(type[0]);
         return a;
     }else{
         var f=type[0];
         var a=type[1];
         var n,m;
         var result=new Array();
         var resulta=new Array();
         if(a.length){
              for(n=0;n<a.length;n++){
                    resulta=a[n].getElementsByTagName("*");
                    for(m=0;m<resulta.length;m++){
                        if(resulta[m].tagName==f.toLocaleUpperCase()){
                             result[result.length]=resulta[m];
                        }
                    }
              }
         }else{
              result=a.getElementsByTagName("*");
              for(m=0;m<resulta.length;m++){
                  if(resulta[m].tagName==f.toLocaleUpperCase()){
                       result[result.length]=resulta[m];
                  }
              }
         }
         return result;
     }     
}

function houdai(x){
     x=x.split(/\s/);//在空格处分开
     var a,result;
     //判断第一个选择器是哪种类型
     a=judgeICT(x[0]);
     if(ById.test(x[1])){
        result=FunById(x[1],a);
     }else if(ByClass.test(x[1])){
        result=FunByClass(x[1],a);
     }else{
        result=FunByTag(x[1],a);
      }
     return result;
     // return x+"是后代选择器";
}

function zidai(x){
 x=x.replace(/\s/,"");
 x=x.split(/>/);
 var a,b;
 var resulta=new Array();
 var resultb=new Array();
 var resultc=new Array();
 var resultd=new Array();
 a=judgeICT(x[0]);
 var n=a.length;
 if(n){//证明n是数组
     var m,k;
     var y=new Array();
     for(m=0;m<n;m++){
        y[m]=a[m].children;//y的每一个元素也是数组                 
     }
     for(m=0;m<y.length;m++){
          if(y[m].length>1){
              for(k=0;k<y[m].length;k++){
                  resulta[resulta.length]=y[m][k];
              }
          }
          else{
              resulta[resulta.length]=y[m];
          }
     }
     for(m=0;m<resulta.length;m++){
         if(resulta[m].length){
             resultb[resultb.length]=resulta[m][0];
         }
         else{
             resultb[resultb.length]=resulta[m];
         }
     }
 }
 else{
     resultb=a.children;
 };
 for(var q=0;q<resultb.length;q++){
      if(resultb[q].length!=0){
            resultd[resultd.length]=resultb[q];
      }
 } 
 // resultd即是父元素下所有非空子元素，下面判断子元素是id/class/tag          
 if(ById.test(x[1])){
       var p;
       var c=x[1].replace(/#/,"");
       c=c.replace(/\s/,"");
       for(p=0;p<resultd.length;p++){
            if(resultd[p].getAttribute("id")==c){
                 resultc[resultc.length]=resultd[p];
            }
       }
 }
 else if(ByClass.test(x[1])){
      var p;
      var c=x[1].replace(/\./,"");
      c=c.replace(/\s/,"");
      for(p=0;p<resultd.length;p++){
            if(resultd[p].getAttribute("class")==c){
                 resultc[resultc.length]=resultd[p];
            }
      }
 }
 else{
     var p;
     var c=x[1].replace(/\s/,"");
     for(p=0;p<resultd.length;p++){
           if(resultd[p].tagName==c.toLocaleUpperCase()){
                resultc[resultc.length]=resultd[p];
           }
     }
 }
 return resultc;
 // return x+"是子代选择器";
}

function jinlin(x){
 x=x.replace(/\s/,"");
 x=x.split(/\+/);
 var a,b,next,n,i;
 var result=new Array();
 a=judgeICT(x[0]);
 if(a.length){
     if(ById.test(x[1])){
          var f=x[1].replace(/#/,"");
          f=f.replace(/\s/,"");
          for(n=0;n<a.length;n++){
              next=a[n].nextSibling;
              for(i=0;i!=1;){
                  if(!next && typeof(next)!="undefined" && next!=0){
                      i=1;
                  }
                  else if((next.nodeType==1)&&(next.getAttribute("id")==f)){
                         result[result.length]=next;
                         i=1;
                  }
                  else{
                     next=next.nextSibling;
                  }
              }    
          }
     }
     else if(ByClass.test(x[1])){
          var f=x[1].replace(/\./,"");
          f=f.replace(/\s/,"");
          for(n=0;n<a.length;n++){
              next=a[n].nextSibling;
              for(i=0;i!=1;){
                  if(!next && typeof(next)!="undefined" && next!=0){
                      i=1;
                  }
                  else if((next.nodeType==1)&&(next.getAttribute("class")==f)){
                         result[result.length]=next;
                         i=1;
                  }
                  else{
                     next=next.nextSibling;
                  }
              }    
          }
     }
     else{
          var f=x[1].replace(/\s/,""); 
          for(n=0;n<a.length;n++){
              next=a[n].nextSibling;
              for(i=0;i!=1;){
                  if(!next && typeof(next)!="undefined" && next!=0){
                      i=1;
                  }
                  else if((next.nodeType==1)&&(next.tagName==f.toLocaleUpperCase())){
                         result[result.length]=next;
                         i=1;
                  }
                  else{
                     next=next.nextSibling;
                  }
              }    
          }
     };
  }
  else{ 
     if(ById.test(x[1])){
        var f=x[1].replace(/#/,"");
        f=f.replace(/\s/,"");
        next=a.nextSibling;
        for(i=0;i!=1;){
            if(!next && typeof(next)!="undefined" && next!=0){
                i=1;
            }
            else if((next.nodeType==1)&&(next.getAttribute("id")==f)){
                   result[result.length]=next;
                   i=1;
            }
            else{
               next=next.nextSibling;
            }
        }    
     }
     else if(ByClass.test(x[1])){
          var f=x[1].replace(/\./,"");
          f=f.replace(/\s/,"");
          next=a.nextSibling;
          for(i=0;i!=1;){
              if(!next && typeof(next)!="undefined" && next!=0){
                  i=1;
              }
              else if((next.nodeType==1)&&(next.getAttribute("class")==f)){
                     result[result.length]=next;
                     i=1;
              }
              else{
                 next=next.nextSibling;
              }
          }    
     }
     else{
          var f=x[1].replace(/\s/,""); 
          next=a.nextSibling;
          for(i=0;i!=1;){
              if(!next && typeof(next)!="undefined" && next!=0){
                  i=1;
              }
              else if((next.nodeType==1)&&(next.tagName==f.toLocaleUpperCase())){
                     result[result.length]=next;
                     i=1;
              }
              else{
                 next=next.nextSibling;
              }
          }    
     }
  }
 //返回的a是数组
 return result;
}

function zhihousuoyou(x){
    x=x.replace(/\s/,"");
    x=x.split(/\~/);
    var a,b,next,n,i;
    var result=new Array();
    a=judgeICT(x[0]);
    if(a.length){
        if(ById.test(x[1])){
            var f=x[1].replace(/#/,"");
            f=f.replace(/\s/,"");
            for(n=0;n<a.length;n++){
                 next=a[n].nextSibling;
                 for(i=0;i!=1;){
                      if(!next && typeof(next)!="undefined" && next!=0){
                        i=1;
                      }
                      else if((next.nodeType==1)&&(next.getAttribute("id")==f)){
                           result[result.length]=next;
                           next=next.nextSibling;
                      }
                      else{
                           next=next.nextSibling;
                      }
                 }
            }
        }
        else if(ByClass.test(x[1])){
            var f=x[1].replace(/\./,"");
            f=f.replace(/\s/,"");
            for(n=0;n<a.length;n++){
                 next=a[n].nextSibling;
                 for(i=0;i!=1;){
                      if(!next && typeof(next)!="undefined" && next!=0){
                         i=1;
                      }
                      else if((next.nodeType==1)&&(next.getAttribute("class")==f)){
                           result[result.length]=next;
                           next=next.nextSibling;
                      }
                      else{
                           next=next.nextSibling;
                      }
                 }
            }
        }
        else{
            var f=x[1].replace(/\s/,""); 
            for(n=0;n<a.length;n++){
                 next=a[n].nextSibling;
                 for(i=0;i!=1;){
                      if(!next && typeof(next)!="undefined" && next!=0){
                          i=1;
                      }
                      else if((next.nodeType==1)&&(next.tagName==f.toLocaleUpperCase())){
                           result[result.length]=next;
                           next=next.nextSibling;
                      }
                      else{
                           next=next.nextSibling;
                      }
                 }
            }
        }
    }
    else{
        if(ById.test(x[1])){
            var f=x[1].replace(/#/,"");
            f=f.replace(/\s/,"");
            next=a.nextSibling;
            for(i=0;i=1;){
                if(!next && typeof(next)!="undefined" && next!=0){
                    i=1;
                }
                else if((next.nodeType==1)&&(next.getAttribute("id")==f)){
                      result[result.length]=next;
                      next=next.nextSibling;
                }
                else{
                      next=next.nextSibling;
                }
            }
        }
        else if(ByClass.test(x[1])){
            var f=x[1].replace(/\./,"");
            f=f.replace(/\s/,"");
            next=a.nextSibling;
            for(i=0;i!=1;){
                if(!next && typeof(next)!="undefined" && next!=0){
                    i=1;
                }
                else if((next.nodeType==1)&&(next.getAttribute("class")==f)){
                      result[result.length]=next;
                      next=next.nextSibling;
                }
                else{
                      next=next.nextSibling;
                }
            }
        }
        else{
            f=x[1].replace(/\s/,"");
            next=a.nextSibling;
            for(i=0;i!=1;){
                if(!next && typeof(next)!="undefined" && next!=0){
                    i=1;
                }
                else if((next.nodeType==1)&&(next.tagName==f.toLocaleUpperCase())){
                      result[result.length]=next;
                      next=next.nextSibling;
                }
                else{
                      next=next.nextSibling;
                }
            }
        }
    }
    return result;
// return x+"是后面所有选择器";
}
